(function() {
  $(function() {
    var all_elements, append_calendar_option, compact_elements, page_perms;
    $('.browse.item.sysmenu').popup({
      popup: '.popup.flowing.sysmenu',
      on: 'click',
      lastResort: "bottom right",
      exclusive: true
    });
    $('.browse.item.notify').popup({
      popup: '.popup.flowing.notify',
      on: 'click',
      lastResort: "bottom right",
      exclusive: true
    });
    $('.dropdown').dropdown().css('min-width', '1em');
    $('.ui.checkbox').checkbox();
    $('.ui.segment#content *').popup({
      exclusive: true,
      delay: {
        show: 10,
        hide: 0
      }
    });
    append_calendar_option = function(calendar, options) {
      var end_with, max_date, min_date, start_with;
      max_date = $(calendar).data('maxdate');
      if (max_date) {
        options.maxDate = new Date(max_date);
      }
      min_date = $(calendar).data('mindate');
      if (min_date) {
        options.minDate = new Date(min_date);
      }
      start_with = $(calendar).data('start-with');
      if (start_with) {
        options.startCalendar = $(start_with);
      }
      end_with = $(calendar).data('end-with');
      if (end_with) {
        options.endCalendar = $(end_with);
      }
      return options;
    };
    $(".ui.calendar.date").each(function() {
      var options;
      options = {
        type: 'date',
        monthFirst: true,
        today: true,
        firstDayOfWeek: 1
      };
      return $(this).calendar(append_calendar_option(this, options));
    });
    $(".ui.calendar.datetime").each(function() {
      var options;
      options = {
        type: 'datetime',
        ampm: false,
        monthFirst: true,
        today: true,
        firstDayOfWeek: 1
      };
      return $(this).calendar(append_calendar_option(this, options));
    });
    $(".ui.calendar.time").each(function() {
      var options;
      options = {
        type: 'time',
        ampm: false,
        today: true,
        startMode: 'hour'
      };
      return $(this).calendar(append_calendar_option(this, options));
    });
    $('.ui.segment#content form input').not('.search input').not('.calendar input').not('.submit').bind('keypress keydown keyup', function(evt) {
      if (evt.keyCode === 13) {
        evt.preventDefault();
        return false;
      }
    });
    page_perms = $('input[name=page_perms]').val();
    window.page_perms = page_perms ? JSON.parse(page_perms) : {};
    all_elements = $('.ui.segment#content .ui').not('.nolayout, .nolayout *').not('option').not('.ui.message');
    compact_elements = all_elements.filter('table.table, button.button');
    switch (Cookies.get('settings.layout_style')) {
      case 'compact':
        all_elements.removeClass('large').addClass('small');
        compact_elements.addClass('compact');
        break;
      case 'normal':
        all_elements.removeClass('small large compact');
        break;
      case 'relax':
        all_elements.removeClass('small compact').addClass('large');
        break;
      default:
        all_elements.removeClass('large').addClass('small');
        compact_elements.addClass('compact');
    }
    $('a#host').click(function(evt) {
      evt.preventDefault();
      return $.post('/hosts/switch', {
        serial: $(this).data('serial'),
        uuid: $(this).data('uuid')
      }, function(resp) {
        if (!resp.succ) {
          return;
        }
        return location.reload(true);
      });
    });
    $('i#refresh_notify').click(function(evt) {
      evt.preventDefault();
      $(this).addClass('loading');
      return $.post('/notify/refresh', null, function(resp) {
        $('i#refresh_notify').removeClass('loading');
        if (!resp.succ) {
          return;
        }
        return location.reload(true);
      });
    });
    $.fn.form.settings.prompt = {
      empty: '不能为空',
      checked: '必须勾选上',
      email: '必须是有效的 EMail 地址',
      url: '必须是有效的 URL 格式',
      regExp: '格式错误',
      integer: '必须为整数，或介于数值区间的整数',
      decimal: '必须为带有小数的数字',
      number: '必须为数字',
      is: '必须是 \'{ruleValue}\'',
      isExactly: '必须精确等于 \'{ruleValue}\'',
      not: '不能设置为 \'{ruleValue}\'',
      notExactly: '不能精确的设置为 \'{ruleValue}\'',
      contain: '不能包含 \'{ruleValue}\'',
      containExactly: '不能精确的包含 \'{ruleValue}\'',
      doesntContain: '不能包含  \'{ruleValue}\'',
      doesntContainExactly: '不能精确的包含 \'{ruleValue}\'',
      minLength: '至少 {ruleValue} 个字符',
      length: '必须 {ruleValue} 个字符',
      exactLength: '长度必须等于 {ruleValue} 个字符',
      maxLength: '不能多于 {ruleValue} 个字符',
      match: '必须与字段 {ruleValue} 值一致',
      different: '必须和字段 {ruleValue} 的值不同',
      creditCard: '必须是有效的信用卡号',
      minCount: '至少要选择 {ruleValue} 项',
      exactCount: '必须选择 {ruleValue} 项',
      maxCount: '必须选择 {ruleValue} 项或更少'
    };
    return $(document).ajaxComplete(function(evt, xhr, settings) {
      if (!xhr.responseJSON) {
        return alert('服务器无响应');
      }
      if (!xhr.responseJSON.succ) {
        alert(xhr.responseJSON.errmsg);
      }
      if (xhr.responseJSON.redirect) {
        return window.location = xhr.responseJSON.redirect;
      }
    });
  });

}).call(this);
